package game.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.google.common.collect.Lists;
import common.controller.base.BaseController;
import common.model.enums.UserProductionTypeEnum;
import common.model.po.GBuilding;
import common.model.po.GCreature;
import common.model.po.GItem;
import common.model.po.GUserProduction;
import common.model.response.base.Result;
import common.util.TreeUtil;
import game.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 物品（包括零件和设备两类）前端控制器
 *
 * @author 米泽鹏
 * @since 2022-02-26
 */
@Api(tags = "game测试")
@RestController
@RequestMapping("/game/test")
public class GameTestController extends BaseController {

	@Resource
	private GItemService itemService;
	@Resource
	private GBuildingService buildingService;
	@Resource
	private GCreatureService creatureService;
	@Resource
	private GUserProductionService userProductionService;
	@Resource
	private TestService testService;

	@ApiOperation(value = "找出物品名称和图片名称不一致的物品", notes = "找出物品名称和图片名称不一致的物品")
	@PostMapping("/test01")
	public Result<?> test01() {
		List<GItem> resultList = Lists.newArrayList();
		List<GItem> itemList = itemService.list();
		for (GItem item : itemList) {
			String image = item.getImage();
			if (StrUtil.isBlank(image)) {
				resultList.add(item);
				continue;
			}
			String itemName = item.getItemName();
			if (!(itemName + ".png").equals(image.split("/")[1])) {
				resultList.add(item);
			}
		}
		return successWithData(resultList);
	}

	@ApiOperation(value = "将物品的图片名称改为与物品名称一致", notes = "将物品的图片名称改为与物品名称一致")
	@PostMapping("/test02")
	public Result<?> test02() {
		List<GItem> updateList = Lists.newArrayList();
		List<GItem> itemList = itemService.list();
		itemList.forEach(item -> {
			String image = item.getImage();
			String itemName = item.getItemName();
			String correctImage = "物品/" + itemName + ".png";
			if (StrUtil.isBlank(image) || !correctImage.equals(image)) {
				item.setImage(correctImage);
				updateList.add(item);
			}
		});
		itemService.updateBatchById(updateList);
		return successWithData(updateList);
	}

	@ApiOperation(value = "批量插入", notes = "批量插入")
	@PostMapping("/test03")
	public Result<?> test03() {
		testService.batchInsert();
		return success();
	}

	@ApiOperation(value = "读取Excel大文件", notes = "读取Excel大文件")
	@PostMapping("/test04")
	public Result<?> test04() {
		testService.readLargeExcel();
		return success();
	}

	@ApiOperation(value = "查询大量数据", notes = "查询大量数据")
	@PostMapping("/test05")
	public Result<?> test05() {
		testService.largeQuery();
		return success();
	}

	@ApiOperation(value = "增量更新", notes = "增量更新")
	@PostMapping("/test06")
	public Result<?> test06() {
		testService.incrementalUpdate();
		return success();
	}

	@ApiOperation(value = "删除其他数据", notes = "删除其他数据")
	@PostMapping("/test07")
	public Result<?> test07() {
		testService.deleteOtherData();
		return success();
	}

	@ApiOperation(value = "修改建筑自定义排序", notes = "如果建筑自定义排序为空或为9999则改为2147483647")
	@PostMapping("/test08")
	public Result<?> test08() {
		List<GBuilding> buildingList = buildingService.list();
		List<GBuilding> updateList = Lists.newArrayList();
		buildingList.forEach(building -> {
			if (building.getCustomSort() == null || building.getCustomSort() == 9999) {
				building.setCustomSort(Integer.MAX_VALUE);
			}
			updateList.add(building);
		});
		buildingService.updateBatchById(updateList);
		return success();
	}

	@ApiOperation(value = "将建筑的图片名称改为与建筑名称一致", notes = "将建筑的图片名称改为与建筑名称一致")
	@PostMapping("/test09")
	public Result<?> test09() {
		List<GBuilding> updateList = Lists.newArrayList();
		List<GBuilding> buildingList = buildingService.list();
		buildingList.forEach(building -> {
			String image = building.getImage();
			String buildingName = building.getBuildingName();
			String correctImage = "建筑/" + buildingName + ".png";
			if (StrUtil.isBlank(image) || !correctImage.equals(image)) {
				building.setImage(correctImage);
				updateList.add(building);
			}
		});
		buildingService.updateBatchById(updateList);
		return successWithData(updateList);
	}

	@ApiOperation(value = "产线收藏数据更新", notes = "产线收藏数据更新")
	@PostMapping("/test10")
	public Result<?> test10() {
		List<GUserProduction> list = Lists.newArrayList();
		List<GUserProduction> userProductionList = userProductionService.list();
		for (GUserProduction userProduction : userProductionList) {
			String data = userProduction.getData();
			if (data.contains("buildingCountStrStr")) {
				userProduction.setData(data.replace("buildingCountStrStr", "buildingCountStr"));
				list.add(userProduction);
			}
		}
		userProductionService.updateBatchById(list);
		return success();
	}

	@ApiOperation(value = "将生物的图片名称改为与生物名称一致", notes = "将生物的图片名称改为与生物名称一致")
	@PostMapping("/test11")
	public Result<?> test11() {
		List<GCreature> updateList = Lists.newArrayList();
		List<GCreature> creatureList = creatureService.list();
		creatureList.forEach(creature -> {
			String image = creature.getImage();
			String creatureName = creature.getCreatureName();
			String correctImage = "生物/" + creatureName + ".png";
			if (StrUtil.isBlank(image) || !correctImage.equals(image)) {
				creature.setImage(correctImage);
				updateList.add(creature);
			}
		});
		creatureService.updateBatchById(updateList);
		return successWithData(updateList);
	}

	@ApiOperation(value = "产线图数据添加overflow属性", notes = "产线图数据添加overflow属性，默认为false，存在则不添加，同时删除多余属性")
	@PostMapping("/test12")
	public Result<?> test12() {
		List<GUserProduction> userProductionList = userProductionService.lambdaQuery().eq(GUserProduction::getType, UserProductionTypeEnum.DIAGRAM).list();
		for (GUserProduction userProduction : userProductionList) {
			String dataString = userProduction.getData();
			JSONObject bean = JSONUtil.toBean(dataString, JSONObject.class);
			// 处理treeData
			JSONArray treeData = bean.getJSONArray("treeData");
			TreeUtil.treeIteratorDown(treeData, node -> {
				JSONObject jsonNode = (JSONObject) node;
				// 添加overflow属性
				jsonNode.putIfAbsent("overflow", false);
				// 删除treeData中的多余属性：buildingVo、byProductList、expandButtonText、expandButtonVisible、expanded、fluid、handmadeBuildingVoList、image、isByProduct、
				// loaded、quantityPerMinute、quantityPerMinuteExactFraction、recipeButtonVisible、recipeId、recipeList、treeNodeId
				jsonNode.remove("buildingVo");
				jsonNode.remove("byProductList");
				jsonNode.remove("expandButtonText");
				jsonNode.remove("expandButtonVisible");
				jsonNode.remove("expanded");
				jsonNode.remove("fluid");
				jsonNode.remove("handmadeBuildingVoList");
				jsonNode.remove("image");
				jsonNode.remove("isByProduct");
				jsonNode.remove("loaded");
				jsonNode.remove("quantityPerMinute");
				jsonNode.remove("quantityPerMinuteExactFraction");
				jsonNode.remove("recipeButtonVisible");
				jsonNode.remove("recipeId");
				jsonNode.remove("recipeList");
				jsonNode.remove("treeNodeId");
			}, o -> ((JSONObject) o).getJSONArray("children"));
			// 处理links
			JSONArray links = bean.getJSONArray("links");
			System.out.println("links = " + links);
			for (Object linkObject : links) {
				JSONObject link = (JSONObject) linkObject;
				// 删除links中的多余属性：__gohashid
				link.remove("__gohashid");
				JSONArray byProducts = link.getJSONArray("byProducts");
				if (CollUtil.isNotEmpty(byProducts)) {
					for (Object byProductObject : byProducts) {
						JSONObject byProduct = (JSONObject) byProductObject;
						// 添加overflow属性
						byProduct.putIfAbsent("overflow", false);
						// 删除byProducts中的多余属性：__gohashid、image、quantityPerMinute、quantityPerMinuteExactFraction、quantityPerProduction
						byProduct.remove("__gohashid");
						byProduct.remove("image");
						byProduct.remove("quantityPerMinute");
						byProduct.remove("quantityPerMinuteExactFraction");
						byProduct.remove("quantityPerProduction");
					}
				}
			}
			userProduction.setData(bean.toString());
		}
		userProductionService.updateBatchById(userProductionList);
		return successWithData(userProductionList);
	}

}
